package com.lsj.juc.myself;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lsj
 * @create 2021-06-16 23:53
 */
class Ticket{
    int ticket = 50;

    Lock lock = new ReentrantLock();

    public void sale(){
        lock.lock();
        try {
            if (ticket >0) {
                System.out.println(Thread.currentThread().getName() + "\t" + "卖出第" + (ticket--) + "张\t还剩" + ticket + "张");
            }
        } finally {
            lock.unlock();
        }
    }
}

public class SaleTest {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(()-> { for (int i = 0; i < 51; i++) { ticket.sale(); }}, "A").start();
        new Thread(()-> { for (int i = 0; i < 51; i++) { ticket.sale(); }}, "B").start();
        new Thread(()-> { for (int i = 0; i < 51; i++) { ticket.sale(); }}, "C").start();
        System.out.println("gitee 1111111");
        System.out.println("local 2222222222");
    }
}
